import {Injectable} from '@angular/core';
import {Resident} from '../types/resp/Resident';
import {ResidentForm} from '../types/req/ResidentForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {ResidentQueryForm} from '../types/req/ResidentQueryForm';

@Injectable({
  providedIn: 'root'
})
export class ResidentService {
  residents: Resident[] = [
    {id: 1, name: '张大小', phone: '13900000111', email: 'zhangsan@qq.com', buildingId: 1, buildingName: '北区1栋', roomId: 1, roomNumber: '301', moveInDate: '2022-01-01', moveOutDate: '', type: 10, createTime: '2024-01-01 12:12:12', modifyTime: '2024-02-02 13:13:13'},
    {id: 2, name: '李四三', phone: '13900000222', email: 'lishishi@qq.com', buildingId: 2, buildingName: '北区2栋', roomId: 1, roomNumber: '402', moveInDate: '2023-02-02', moveOutDate: '', type: 10, createTime: '2024-02-02 12:12:12', modifyTime: '2024-03-03 13:13:13'},
    {id: 3, name: '王无语', phone: '13900000333', email: 'wangwuyu@qq.com', buildingId: 2, buildingName: '北区2栋', roomId: 1, roomNumber: '503', moveInDate: '2024-03-03', moveOutDate: '', type: 10, createTime: '2024-03-03 12:12:12', modifyTime: '2024-04-04 13:13:13'},
    {id: 4, name: '赵元长', phone: '13900000444', email: 'zhaoyuan@qq.com', buildingId: 3, buildingName: '南区1栋', roomId: 1, roomNumber: '604', moveInDate: '2022-04-04', moveOutDate: '', type: 20, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 5, name: '钱一一', phone: '13900000555', email: 'qianyiyi@qq.com', buildingId: 3, buildingName: '南区1栋', roomId: 1, roomNumber: '204', moveInDate: '2023-04-04', moveOutDate: '', type: 10, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 6, name: '王双双', phone: '13900000666', email: 'wushuang@qq.com', buildingId: 4, buildingName: '东区1栋', roomId: 1, roomNumber: '304', moveInDate: '2024-04-04', moveOutDate: '', type: 10, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 7, name: '郑艳南', phone: '13900000777', email: 'zhengyan@qq.com', buildingId: 4, buildingName: '东区1栋', roomId: 1, roomNumber: '404', moveInDate: '2021-04-04', moveOutDate: '', type: 20, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'}
  ]
  constructor() {
  }

  save (residentForm: ResidentForm): Observable<Result<void>> {
    console.log('residentApi save params', residentForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (residentQueryForm: ResidentQueryForm): Observable<Result<Page<Resident>>> {
    console.log('residentApi find params', residentQueryForm)
    return MockApi.queryPageSuccessfully(this.residents).pipe(
      map((response: Response<Result<Page<Resident>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Resident>> {
    console.log('residentApi findById id', id)
    let target = {}
    for (const a of this.residents) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Resident>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('residentApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
